package com.example.viewpager2

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import java.io.BufferedInputStream
import java.net.HttpURLConnection
import java.net.URL


class DownLoadImageView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {


    private var imageViewW = 0f
    private var imageViewH = 0f
    private var grayCanvas :Canvas ?= null

    private val touchPath = Path()
//    private val bgPaint = Paint().apply {
//        color = Color.GRAY
//    }

    private val mPathPaint = Paint().apply {
        setAlpha(0)
//        color = Color.RED
        isAntiAlias = true
        style = Paint.Style.STROKE
        strokeCap = Paint.Cap.ROUND
        strokeJoin = Paint.Join.ROUND
        strokeWidth = 80f
        setXfermode(PorterDuffXfermode(PorterDuff.Mode.SRC_IN))
    }



    private var bitmap :Bitmap?=null

    private var bgBitmap:Bitmap?=null


    fun setImageUrl(url:String) {

       Thread(object :Runnable{
           override fun run() {
               try {
                   val url = URL(url)
                   val openConnection = url.openConnection()
                   val coon = openConnection as HttpURLConnection
                   val length = coon.contentLength
                   coon.connect()
                   val input = coon.inputStream
                   val bufferInputStream = BufferedInputStream(input,length)
                   bitmap = BitmapFactory.decodeStream(bufferInputStream)
                   bufferInputStream.close()
                   input.close()
                   invalidate()
               }catch (e:Exception){
                   e.printStackTrace()
               }
           }
       }).start()
    }

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)
        imageViewW = w.toFloat()
        imageViewH = h.toFloat()

        bgBitmap = Bitmap.createBitmap(imageViewW.toInt(),imageViewH.toInt(),Bitmap.Config.ARGB_8888)
        grayCanvas = Canvas(bgBitmap!!)
        grayCanvas?.drawColor(Color.GRAY)  //绘制灰色的背景
    }


    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        canvas?.apply {
            //绘制底图
            drawBitmap(this)
            //绘制蒙版
            drawBg(this)
            //绘制路径
            drawPath(this)
        }
    }

    private fun drawPath(canvas: Canvas) {

        canvas.drawPath(touchPath,mPathPaint)
    }

    private fun drawBg(canvas: Canvas) {
        bgBitmap?.let {
            canvas.drawBitmap(it,0f,0f,null)
        }
    }

    private fun drawBitmap(canvas: Canvas) {
        bitmap?.let {
            canvas.drawBitmap(it,0f,0f,null)
        }
    }

    private var moveX = 0f
    private var moveY = 0f

    override fun onTouchEvent(event: MotionEvent?): Boolean {
//        Log.e("日志", "onTouchEvent: x:${event?.x}")
         event?.let {
             moveX = it.x
             moveY = it.y
             Log.e("日志", "onTouchEvent: ${it.action}")
             when(it.action){
                 MotionEvent.ACTION_DOWN->{

                     touchPath.moveTo(moveX,moveY)
                     invalidate()
                     return true
                 }
                 MotionEvent.ACTION_MOVE->{
                     Log.e("日志", "onTouchEvent: ")
                     val endX = it.x
                     val endY = it.y
                     touchPath.quadTo((endX - moveX) / 2 + moveX, (endY - moveY) / 2 + moveY, endX, endY);
                     invalidate()
                     return true
                 }
                 else->{
                     return super.onTouchEvent(event)
                 }
             }

         }


        return super.onTouchEvent(event)

    }


}